package com.iss.ljx.menu.service.impl;

import com.iss.ljx.menu.repository.MenuDao;
import com.iss.ljx.menu.service.MenuService;
import com.iss.ljx.mybatis.entity.PtMenuEntity;
import com.iss.ljx.mybatis.ztree.ZtreeEntity;
import com.iss.ljx.role.repository.RoleDao;
import com.iss.ljx.util.DataUtil;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Locale;

@Service("menuService")
public class MenuServiceImpl implements MenuService {

    @Resource
    private RoleDao roleDao;
    @Value("${ubound.uuid}")
    private String uboundUuid;
    @Resource
    private MessageSource messageSource;

    @Override
    public List<ZtreeEntity> getMenuTree() {
        List<ZtreeEntity> list = roleDao.queryMenuTree();
        ZtreeEntity ZtreeEntity = new ZtreeEntity();
        ZtreeEntity.setId(uboundUuid);
        Locale locale = LocaleContextHolder.getLocale();
        String unboundName = messageSource.getMessage("common.unbound.name", null, locale);
        ZtreeEntity.setName(unboundName);
        list.add(ZtreeEntity);
        Integer index = 0;
        for (ZtreeEntity item : list) {
            if (index < 10) {
                item.setOpen(true);
            } else {
                item.setOpen(false);
            }
            index++;
        }

        return list;
    }

    @Resource
    private MenuDao menuDao;

    @Override
    public PtMenuEntity getMenuInfoById(String menuId) {
        return menuDao.getMenuInfoById(menuId);
    }

    @Override
    public void modifyMenuInfoById(PtMenuEntity entity) {
        menuDao.modifyMenuInfoById(entity);
    }

    @Override
    public void addMenuInfoById(PtMenuEntity entity) {
        entity.setMenuId(DataUtil.getUUIDShort());
        entity.setParentId(uboundUuid);

        menuDao.addMenuInfoById(entity);
    }

    @Override
    public void deleteMenuInfoById(String menuId) {
        menuDao.deleteMenuInfoById(menuId);
    }

    @Override
    public List<PtMenuEntity> getUserMenu(String userId) {
        return menuDao.getUserMenu(userId);
    }

    @Override
    public String getUserRoleId(String userId) {
        return menuDao.getUserRoleId(userId);
    }
    @Override
    public Integer setUserMenu(String roleId, String menuId){
        return  menuDao.setUserMenu(roleId, menuId);
    }
    @Override
    public Integer delUserMenu(String roleId, String menuId){
        return  menuDao.delUserMenu(roleId, menuId);
    }
}
